package com.work.crops.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.work.crops.pojo.CropsDisease;
import com.work.crops.pojo.CropsParam;
import com.work.crops.service.CropsDiseaseService;
import com.work.entity.PageResult;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cropsDisease")
public class CropsDiseaseController {

    @Autowired
    private CropsDiseaseService cropsDiseaseService;

//    根据id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id ){
        return cropsDiseaseService.findById(id);
    }

//    根据农作物id查询病虫害数据
    @GetMapping("/find/{id}")
    public Result<List<CropsDisease>> findByCropsId(@PathVariable("id") Long id){
        try {
            List<CropsDisease> list = cropsDiseaseService.findByCropsId(id);
            return new Result(true,StatusCode.FIND_Ok,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.FIND_ERROR,"查询失败");
        }

    }

//    分页条件查询
    @GetMapping("/list/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                                 @PathVariable Integer size,
                                 CropsParam cropsParam){


//        判断 条件对象是否为空
        Map map = JSON.parseObject(JSON.toJSONString(cropsParam), Map.class);
        Collection values = map.values();
        if (values.size() == 0){
//            如果长度为0增加标识
            cropsParam.setEmpty("alin");
        }

        PageInfo pageInfo = cropsDiseaseService.findByPage(page, size, cropsParam);

        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result(pageResult != null?true:false,
                pageResult != null? StatusCode.FIND_Ok:StatusCode.FIND_ERROR,
                pageResult != null ?"查询成功":"查询成功",
                pageResult);
    }


//    删除病害虫数据
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        Integer row = cropsDiseaseService.deleteById(id);

        return new Result(row != null,
                row != null? StatusCode.ADD_OK:StatusCode.ADD_ERROR,
                row != null ? "删除成功" : "删除失败");
    }

//    根据id修改病虫害数据
    @PutMapping("/edit")
    public Result edit(@RequestBody CropsDisease cropsDisease){
        try {
             cropsDiseaseService.updateById(cropsDisease);
            return new Result(true,StatusCode.UPDATE_Ok,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.UPDATE_ERROR,"修改失败");
        }

    }


//    添加病虫害同时绑定农作物
    @PostMapping("/add/{cropsId}")
    public Result addCropsDisease(@RequestBody CropsDisease cropsDisease,
                                  @PathVariable Integer cropsId,
                                  String stage){
        try {
            cropsDiseaseService.add(cropsDisease,cropsId,stage);
            return new Result(true,StatusCode.ADD_OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ADD_ERROR,"添加失败");
        }
    }




}
